package com.baidu.yyb.actiivitys;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.yyb.MMKV.NoteSp;
import com.baidu.yyb.R;
import com.baidu.yyb.bean.UserData;
import com.baidu.yyb.constant.NoteTag;
import com.baidu.yyb.constant.StringName;
import com.baidu.yyb.prensenter.NotePresenter;
import com.lt.utils.String.StringUtils;
import com.lt.utils.ViewUtils.ToastUtils;
import com.lt.utils.ViewUtils.ViewUtils;

/**
 * @author LiuTong
 */
public class NoteStartActivity extends BaseActivity {

    private Button btn;
    private EditText etUserName,etPassword,etPhoneNumber;
    private TextView btnRegister,btnLostPassword,btnJump,tvLoginTip;
    private NoteTag.SHOW_MODE showMode = NoteTag.SHOW_MODE.login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void setActivityLayout() {
        super.setActivityLayout();
        setContentView(R.layout.activity_note_start);
    }

    @Override
    public void initView() {
        super.initView();
        btn = findViewById(R.id.btn);
        etUserName = findViewById(R.id.edit_use_name);
        etPassword = findViewById(R.id.edit_password);
        etPhoneNumber = findViewById(R.id.edit_phone);
        btnRegister = findViewById(R.id.btn_register);
        btnLostPassword = findViewById(R.id.btn_lost_password);
        btnJump = findViewById(R.id.btn_jump);
        tvLoginTip = findViewById(R.id.tv_login_or_register);
    }

    @Override
    public void initData() {
        super.initData();
    }


    @Override
    public void initEvent() {
        super.initEvent();
        btn.setOnClickListener(v -> {
            if(showMode == NoteTag.SHOW_MODE.login){
                /***
                 * 登录
                 */
                notePresenter.doLogin();
            }else if(showMode == NoteTag.SHOW_MODE.register){
                /***
                 * 注册
                 */
                notePresenter.doRegister();
            }else if(showMode == NoteTag.SHOW_MODE.lost){
                notePresenter.doLost();
            }
        });

        btnJump.setOnClickListener(v -> {
            goActivity(MainActivity.class,null,true);
        });
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkInput()){
                    return;
                }
                showRegister();
            }
        });
        btnLostPassword.setOnClickListener(v -> {

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        notePresenter.showDisplayMode();
    }

    @Override
    public void showLogin(Bundle bundle) {
        tvLoginTip.setText(getResources().getString(R.string.login));
        ViewUtils.setViewGone(etPhoneNumber);
        ViewUtils.setViewVisible(btnRegister,btnLostPassword);
        showMode = NoteTag.SHOW_MODE.login;
        if(bundle != null){
            UserData userData = bundle.getParcelable(NoteTag.USERDATA);
            if(!userData.isNull()){
                etUserName.setText(userData.getUserId());
                etPassword.setText(userData.getPassword());
                etPhoneNumber.setText(userData.getPhoneNumber());
            }

        }
    }

    @Override
    public void showToast(String toast) {
        ToastUtils.showToast(mContext,toast);
    }

    @Override
    public UserData getUserData() {
        return new UserData(etUserName.getText().toString(),etPassword.getText().toString(),etPhoneNumber.getText().toString());
    }

    @Override
    public void showActivityMode() {
        if(NoteSp.getBoolean(NoteTag.LOCK)){
            /***
             * 验证指纹，成功 跳转主页面
             */
            goActivity(null,null,true);
        }else if(!StringUtils.isEmptyOrNull(NoteSp.getString(NoteTag.USER_NAME))
                && !StringUtils.isEmptyOrNull(NoteSp.getString(NoteTag.PASSWORD))){
            goActivity(MainActivity.class,null,true);
        }else {
            showLogin(null);
        }
    }

    @Override
    public void showRegister() {
        tvLoginTip.setText(getResources().getString(R.string.register));
        ViewUtils.setViewGone(btnRegister,btnLostPassword);
        ViewUtils.setViewVisible(etPhoneNumber);
        showMode = NoteTag.SHOW_MODE.register;
    }

    @Override
    public boolean checkInput() {
        StringBuilder sb = new StringBuilder();
        String useName = String.valueOf(etUserName.getText());
        String password = String.valueOf(etPassword.getText());
        String phone = String.valueOf(etPhoneNumber.getText());
        boolean isOver = false;

        if(!StringUtils.isEmptyOrNull(useName)){
            if(useName.length() < StringName.MIN_LIMIT || useName.length() > StringName.MAX_USERNAME){
                sb.append(getResources().getString(R.string.inputUserNameLimitTip)).append("\n");
                isOver = true;
            }
        }else {
            sb.append(getResources().getString(R.string.inputUserNameNullTip)).append("\n");
            isOver = true;
        }

        if(!StringUtils.isEmptyOrNull(password)){
            if(password.length() < StringName.MIN_LIMIT || password.length() > StringName.MAX_PASSWORD){
                sb.append(getResources().getString(R.string.inputUserNameLimitTip)).append("\n");
                isOver = true;
            }
        }else {
            sb.append(getResources().getString(R.string.inputPasswordNullTip)).append("\n");
            isOver = true;
        }

        if(!StringUtils.isEmptyOrNull(phone)){
            if(phone.length() != StringName.PHONE_NUMBER_LIMIT){
                sb.append(getResources().getString(R.string.inputPhoneLimitTip)).append("\n");
                isOver = true;
            }
        }else {
            sb.append(getResources().getString(R.string.inputPhoneNullTip)).append("\n");
            isOver = true;
        }

        ToastUtils.showToast(mContext,sb.toString());
        return isOver;

    }

    @Override
    public void showLost() {
        /****
         * 显示找回密码页面
         */
        showMode = NoteTag.SHOW_MODE.lost;
    }
}